/**
 * Created by xu on 2015/12/23.
 */

function getMenu(){
    if ((typeof pageName_)==null ){
        $("#leftBar").hide();
        return;
    }

    if ($.type(path)!='string'){
        path=path__;
    }
    var dataa={"menuMark":menuMark_};
    $("#leftBar").empty();
    $().invoke(path+"/menu/menuList.json",dataa,[function(m,r){
        var b=false;
        //先将一级目录加进去
        for(var i in r){
            if (r[i].id==menuMark_){
                if (!b){
                    $('#logoImg').prop({"src":(resourcePath+(r[i].url)),"alt":(r[i]['name'])});
                    b=true;
                }
            }
            if (r[i].parentId==menuMark_){
                var cssc=r[i].css;
                var icss="";
                if (cssc!=null&&cssc.indexOf("active")>-1){
                    icss="active";
                }
                var divv="<div mid='"+(r[i].id)+"' class='left-menu '>"+
                    "<a class=\"parent-menu "+icss+"\" href=\"javascript:void(0)\">"+
                    "<em class='"+cssc+"'></em>"+((r[i].showName==null||r[i].showName=='')?r[i].name:r[i].showName)+"<span><i class="+icss+"></i></span>"+
                    "</a>"+"</div>";
                $("#leftBar").append(divv);
            }
            // $("a[menuId="+(r[i].id)+"]").unbind("click").prop("href",(path+(r[i].url)))
        }

        //加入子菜单scroll-left
        for(var i in r){
            var ele=r[i];
            var pdom=$(".left-menu[mid='"+(ele.parentId)+"']");
            if (pdom.find("ul").length==0){
                var ule="<ul  class='submenu  none'>";
                ule+="</ul>";
                $(".left-menu[mid='"+(ele.parentId)+"']").append(ule);
            }else {
                var ul_=pdom.find("ul").eq(0);
                if (ul_.find("li").length>3){
                    if (!ul_.hasClass("scroll-left")){
                        ul_.addClass("scroll-left");
                    }
                }
            }

            var uleli=""+
                "<li onclick='subMenuClickUl(this)'>" +
                "<a id_='"+(ele.id)+"' pid_='"+(ele.parentId)+"'  hreft="+(path+ele.url)+" ><i></i>"+((ele.showName==null||ele.showName=='')?ele.name:ele.showName)+"</a>" +
                "</li>"+
                "";

            $("ul",pdom).append(uleli);
        }




        try {
            var midd = localStorage.getItem("menuActive_");
            var middsub = localStorage.getItem("menuActive_subMenu_");

            if (midd != null && midd != '') {
                var cc = $(".left-menu[mid='" + (midd) + "'] .parent-menu");
                if (cc.length>0 && !cc.hasClass("active")) {
                    //cc.trigger("click");
                    $(".left-menu .submenu").hide();
                    $("#leftBar .active").removeClass("active");
                    cc.siblings().show();
                    cc.addClass("active");
                    cc.find("em").addClass("active");
                }
                $(".submenu a[id_='" + (middsub) + "']").parent().addClass("active");
                //localStorage.setItem("menuActive_", "");
                //localStorage.setItem("menuActive_subMenu_", "");
            }
        }catch (e){console.log(e)}

    }]);
}

function subMenuClickUl(ul){
    var a=$("a[hreft]",ul);
    subMenuClick(a);
}

function subMenuClick(a){
    //console.log($.cookie('menuActive_')+">>>1");
    try {
        //var aParentMid=$(this.parentNode).attr("mid");
        //$.cookie('menuActive_',pid,{expires:1});
        var id_=$(a).attr("id_");
        var pid=$(a).attr("pid_");
        localStorage.setItem("menuActive_", pid);
        localStorage.setItem("menuActive_subMenu_", id_);
    }catch (e){console.log(e)}
    var zUrl=$(a).attr("hreft");
    if (zUrl==null || zUrl=='' || zUrl==path){
        alert("还没有配置此菜单")
        return;
    }
    top.location=zUrl;
}

$(document).ready(function(){
    if ((typeof localStorage)=='undefined'){
        alert("注意，您的浏览器版本过低，部分功能可能异常，请使用高版本浏览器");
    }
    getMenu();
    /*$("#leftBarM").scroll(function(){
        if(this.scrollHeight>this.clientHeight||this.offsetHeight>this.clientHeight){
            $("#aboutme").hide()
        }
    });*/
});